package com.example.park.addpark

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.entity.ParkBeanItem
import com.example.lib_base.mvvm.BaseActivity
import com.example.park.R
import com.example.park.databinding.ActivityParkEtBinding
import com.example.park.park.ParkViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/park/ParkEtActivity")
class ParkEtActivity : BaseActivity<ActivityParkEtBinding,ParkViewModel>() {
    @Autowired
    @JvmField
    var pid:String=""

    var area="A区停车场"
    var type="标准车位"
    var areaId=1
    var status="空闲"
    override fun initView() {
        ARouter.getInstance().inject(this)
        vm.getParkById(pid.toInt())
        binding.parkaddAdd.setOnClickListener {
            val carNo = binding.parkaddCarNo.text.toString()
            val remark = binding.parkaddRemark.text.toString()
            val user = binding.parkaddUser.text.toString()
            val carNum = binding.parkaddCarNum.text.toString()
            val item =ParkBeanItem(areaId,area,null,carNum,remark,carNo,status,type,0,user)
            vm.update(pid.toInt(),item)
        }
        binding.parkaddRg.setOnCheckedChangeListener { radioGroup, i ->
            when(i){
                R.id.rb_kx->status="空闲"
                R.id.rb_zy->status="已占用"
                else->status="维修中"
            }
        }
    }

    override fun initData() {
        vm.getParkByIdData.observe(this){
            binding.parkaddCarNo.setText(it.spaceNumber)
            when(it.spaceStatus){
                "空闲"->binding.parkaddRg.check(R.id.rb_kx)
                "已占用"->binding.parkaddRg.check(R.id.rb_zy)
                "维修中"->binding.parkaddRg.check(R.id.rb_wh)
            }
            binding.parkaddUser.setText(it.userName)
            binding.parkaddCarNum.setText(it.licensePlate)
            binding.parkaddRemark.setText(it.remarks)

            val areas= mutableListOf<String>("A区停车场","B区停车场","C停车场")
            if (!areas.contains(it.areaName)){
                areas.add(0,it.areaName)
            }
            val areaAdapter =ArrayAdapter(this,android.R.layout.simple_spinner_item,areas)
            areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            binding.parkaddArea.adapter=areaAdapter
            binding.parkaddArea.onItemSelectedListener=object :AdapterView.OnItemSelectedListener{
                override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                    area=areas[p2]
                    areaId=p2+1
                }

                override fun onNothingSelected(p0: AdapterView<*>?) {

                }
            }

            val types = mutableListOf<String>("标准车位","VIP车位")
            if (!types.contains(it.spaceType)){
                types.add(0,it.spaceType)
            }
            val typeAdapter =ArrayAdapter(this,android.R.layout.simple_spinner_item,types)
            typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            binding.parkaddType.adapter=typeAdapter
            binding.parkaddType.onItemSelectedListener=object :AdapterView.OnItemSelectedListener{
                override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                    type=types[p2]
                }

                override fun onNothingSelected(p0: AdapterView<*>?) {

                }
            }
        }
        vm.updateSuccess.observe(this){
            ToastUtils.showLong("编辑成功")
            finish()
        }

    }

}